package code2;

class Solution {
    /**
     * 三路排序方法--解决力扣75
     */

    public void sortColors(int[] nums) {

        int zero = -1 ;
        int two = nums.length;

        for (int i = 0; i < two; ) { //注意这里的i自增没有在这里控制
            if(nums[i]==1){
                i++;
            }
            else 
            if(nums[i] == 2) {
                two--;
                swap(nums,i,two);

            }else{
                zero++;
                swap(nums,zero,i);
                i++;
            
            }
        }

    }
    
    private void swap(int [] nums,int i, int j){
        int temp = nums[i];
        nums[i]  = nums[j];
        nums[j]  = temp;
    }
}